merged [20982]: Fixed capitilization of MySpace in all places, and enabled the MySpac...
[adiumx.git] / Plugins / Link Management / SHLinkManagementPlugin.m
blob79ed50fbac9e042bdf28fd7deca601c22e6b6518
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import <Adium/AIMenuControllerProtocol.h>
18 #import <Adium/AIToolbarControllerProtocol.h>
19 #import "SHAutoValidatingTextView.h"
20 #import "SHLinkEditorWindowController.h"
21 #import "SHLinkManagementPlugin.h"
22 #import <AIUtilities/AIMenuAdditions.h>
23 #import <AIUtilities/AIStringAdditions.h>
24 #import <AIUtilities/AIToolbarUtilities.h>
25 #import <AIUtilities/AIImageAdditions.h>
26 #import <AIUtilities/AIWindowAdditions.h>
28 #define ADD_LINK_TITLE                  [AILocalizedString(@"Add Link",nil) stringByAppendingEllipsis]
29 #define EDIT_LINK_TITLE                 [AILocalizedString(@"Edit Link",nil) stringByAppendingEllipsis]
30 #define RM_LINK_TITLE           AILocalizedString(@"Remove Link",nil)
32 @interface SHLinkManagementPlugin (PRIVATE)
33 - (BOOL)textViewSelectionIsLink:(NSTextView *)textView;
34 - (void)registerToolbarItem;
35 @end
37 @implementation SHLinkManagementPlugin
39 - (void)installPlugin
41         NSMenuItem      *menuItem;
42         
43     //Add/Edit Link... menu item (edit menu)
44     menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:EDIT_LINK_TITLE
45                                                                                                                                          target:self
46                                                                                                                                          action:@selector(editFormattedLink:)
47                                                                                                                           keyEquivalent:@"k"] autorelease];
48     [[adium menuController] addMenuItem:menuItem toLocation:LOC_Edit_Links];
49     
50     //Context menu
51     menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:EDIT_LINK_TITLE
52                                                                                                                                          target:self
53                                                                                                                                          action:@selector(editFormattedLink:)
54                                                                                                                           keyEquivalent:@""] autorelease];
55     [[adium menuController] addContextualMenuItem:menuItem toLocation:Context_TextView_LinkEditing];
56     [self registerToolbarItem];
57         
58     //rm link context
59     menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:RM_LINK_TITLE
60                                                                                                                                          target:self
61                                                                                                                                          action:@selector(removeFormattedLink:)
62                                                                                                                           keyEquivalent:@""] autorelease];
63     [[adium menuController] addContextualMenuItem:menuItem toLocation:Context_TextView_LinkEditing];
66 - (void)uninstallPlugin
68         
71 //Update our add/edit link menu item
72 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
74         NSResponder     *responder = [[[NSApplication sharedApplication] keyWindow] firstResponder];
75         if (responder && [responder isKindOfClass:[NSTextView class]]) {
76                 if ([[menuItem title] isEqualToString:RM_LINK_TITLE]) {
77                         // only make remove link menu item active if slection is a link.
78                         return [self textViewSelectionIsLink:(NSTextView *)responder];
79                 } else {
80                         //Update the menu item's title to reflect the current action
81                         [menuItem setTitle:([self textViewSelectionIsLink:(NSTextView *)responder] ? EDIT_LINK_TITLE : ADD_LINK_TITLE)];
82                         
83                         return [(NSTextView *)responder isEditable];
84                 }
85         } else {
86                 return NO; //Disable the menu item if a text field is not key
87         }
88         
91 //Add or edit a link
92 - (IBAction)editFormattedLink:(id)sender
94         NSWindow        *keyWindow = [[NSApplication sharedApplication] keyWindow];
95         NSTextView      *earliestTextView = (NSTextView *)[keyWindow earliestResponderOfClass:[NSTextView class]];
97     if (earliestTextView &&
98                 ![[keyWindow windowController] isKindOfClass:[SHLinkEditorWindowController class]]) {
99                 [SHLinkEditorWindowController showLinkEditorForTextView:earliestTextView
100                                                                                                            onWindow:keyWindow
101                                                                                                   showFavorites:YES
102                                                                                                 notifyingTarget:nil];
103     }
106 - (IBAction)removeFormattedLink:(id)sender
108         NSWindow        *keyWindow = [[NSApplication sharedApplication] keyWindow];
109         NSTextView      *earliestTextView = (NSTextView *)[keyWindow earliestResponderOfClass:[NSTextView class]];
110     
111         if (earliestTextView) {
112                 NSRange selectedRange = [earliestTextView selectedRange];
113                 id              selectedLink = nil;
114                 
115                 if ([[earliestTextView textStorage] length] &&
116                         selectedRange.location != NSNotFound &&
117                         selectedRange.location != [[earliestTextView textStorage] length]) {
118                         
119                         selectedLink = [[earliestTextView textStorage] attribute:NSLinkAttributeName
120                                                                                                                          atIndex:selectedRange.location
121                                                                                                           effectiveRange:&selectedRange];
122                         [[earliestTextView textStorage] removeAttribute:NSLinkAttributeName range:selectedRange];                       
123                 }
124         }       
127 //Returns YES if a link is under the selection of the passed text view
128 - (BOOL)textViewSelectionIsLink:(NSTextView *)textView
130         id              selectedLink = nil;
131         
132         if ([[textView textStorage] length] &&
133            [textView selectedRange].location != NSNotFound &&
134            [textView selectedRange].location != [[textView textStorage] length]) {
135                 NSRange selectionRange = [textView selectedRange];
136                 selectedLink = [[textView textStorage] attribute:NSLinkAttributeName
137                                                                                                  atIndex:selectionRange.location
138                                                                                   effectiveRange:&selectionRange];
139         }
140         
141         return selectedLink != nil;
144 #pragma mark Toolbar Item stuff
146 - (void)registerToolbarItem
148     //Unregister the existing toolbar item first
149     if (toolbarItem) {
150         [[adium toolbarController] unregisterToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
151         [toolbarItem release]; toolbarItem = nil;
152     }
153     
154     toolbarItem = [[AIToolbarUtilities toolbarItemWithIdentifier:@"LinkEditor"
155                                                            label:AILocalizedString(@"Link",nil)
156                                                     paletteLabel:AILocalizedString(@"Insert Link",nil)
157                                                          toolTip:AILocalizedString(@"Add/Edit Hyperlink",nil)
158                                                           target:self
159                                                  settingSelector:@selector(setImage:)
160                                                      itemContent:[NSImage imageNamed:@"linkToolbar" forClass:[self class]]
161                                                           action:@selector(editFormattedLink:)
162                                                             menu:nil] retain];
163     
164     [[adium toolbarController] registerToolbarItem:toolbarItem forToolbarType:@"TextEntry"];
166 @end